﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Data;
using AutoMapper;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.ServiceLocation;
using Sautom.ClientComunication;
using Sautom.ClientComunication.CommandService;
using Sautom.ClientComunication.Events;
using Sautom.ClientComunication.QueriesService;
using Sautom.ClientComunication.Services;
using Sautom.Modules.Proposal.Models;

namespace Sautom.Modules.Proposal.ViewModels
{
    public sealed class CountryEditViewModel : NavigationAwareNotificationObject
    {
        #region Constructor
        public CountryEditViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            RegionManager = regionManager;

            SaveCountryCommand = new DelegateCommand<string>(ExecuteSaveCountryCommand);
            AddCityCommand = new DelegateCommand<string>((cityName) =>
                                                             {
                                                                 if (!string.IsNullOrWhiteSpace(cityName))
                                                                     Cities.Add(new CityItemDtoOutput() { CityName = cityName });
                                                             });
            AddDocumentCommand = new DelegateCommand<string>((docName) =>
                                                                 {
                                                                     if (!string.IsNullOrWhiteSpace(docName))
                                                                         EmbassyDocuments.Add(
                                                                             new EmbassyDocumentItemDtoOutput() { DocumentName = docName });
                                                                 });

            RemoveCityFromList = new DelegateCommand<string>(cityName => Cities.Remove(Cities.First(rec => rec.CityName == cityName)));
            //RemoveDocFromList = new DelegateCommand<string>(docName => EmbassyDocuments.Remove(EmbassyDocuments.First(rec => rec.DocumentName == docName)));

            QueriesServiceClient = ServiceFactory.Get(eventAggregator).GetQueriesService();
            CommandsServiceClient = ServiceFactory.Get(eventAggregator).GetCommandsService();

            Cities = new ObservableCollection<CityItemDtoOutput>();
            EmbassyDocuments = new ObservableCollection<EmbassyDocumentItemDtoOutput>();
        }


        #endregion

        #region Properties
        private IRegionManager RegionManager { get; set; }
        private IQueriesService QueriesServiceClient { get; set; }
        private ICommandService CommandsServiceClient { get; set; }

        #endregion

        #region Commands
        public DelegateCommand<string> SaveCountryCommand { get; private set; }
        public DelegateCommand<string> AddCityCommand { get; private set; }
        public DelegateCommand<string> AddDocumentCommand { get; private set; }
        public DelegateCommand<string> RemoveCityFromList { get; private set; }
        //public DelegateCommand<string> RemoveDocFromList { get; private set; }
        

        private void ExecuteSaveCountryCommand(string isSave)
        {
            if (isSave == "true")
            {
                Mapper.CreateMap<CountryEditViewModel, CounrtyEditDtoInput>();
                Mapper.CreateMap<CityItemDtoOutput, CityItemDtoInput>();
                Mapper.CreateMap<EmbassyDocumentItemDtoOutput, EmbassyDocumentItemDtoInput>();
                var countryData = Mapper.Map<CounrtyEditDtoInput>(this);

                CommandsServiceClient.EditOrAddCountry(countryData);
            }

            RegionManager.RequestNavigate(RegionProvider.MainRegion, PathProvider.CountriesList);
        }

        #endregion

        #region INavigationAware implementation
        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            // Initial load - Load based on ID passed in
            string id = navigationContext.Parameters["countryId"];
            if (string.IsNullOrWhiteSpace(id)) return;

            CountryEditDtoOutput country = QueriesServiceClient.GetCountryForEdit(Guid.Parse(id));
            Mapper.CreateMap<CountryEditDtoOutput, CountryEditViewModel>();
            Mapper.Map(country, this);

            Cities = new ObservableCollection<CityItemDtoOutput>(country.Cities);
            EmbassyDocuments = new ObservableCollection<EmbassyDocumentItemDtoOutput>(country.EmbassyDocuments);
        }
        #endregion

        #region Country properties

        public Guid Id { get; set; }

        public ObservableCollection<CityItemDtoOutput> Cities { get; set; }
        public ObservableCollection<EmbassyDocumentItemDtoOutput> EmbassyDocuments { get; set; }

        private string _countryName;
        public string CountryName
        {
            get { return _countryName; }
            set
            {
                _countryName = value;
                RaisePropertyChanged(() => CountryName);
            }
        }

        #endregion
    }
}